/******************************************************************************
*  author: hnuweiwei@gmail.com
*  version: 1.0
*  file: myStack2.h
*  date: 2014.05.25
*  description: 栈的头文件声明与定义
*  other: 对类模板进行特化，如果要特化一个类模板就需要特化改类模板的所有
          成员函数，为了特化一个类模板起始处声明一个template<>
******************************************************************************/

#ifndef MYSTACK2_H_INCLUDED
#define MYSTACK2_H_INCLUDED

#include <deque>
#include <string>
#include <stdexcept>
#include "myStack.h"

template<>
class MyStack<std::string> {
  private:
    std::deque<std::string> elems;  // elements

  public:
    void push(std::string const&);  // push element
    std::string pop();                     // pop element
    std::string top() const;        // return top element
    bool empty() const {            // return whether the stack is empty
        return elems.empty();
    }
};

void MyStack<std::string>::push (std::string const& elem)
{
    cout<<"string special push method..."<<endl;
    elems.push_back(elem);    // append copy of passed elem
}

std::string MyStack<std::string>::pop ()
{
    if (elems.empty()) {
        throw std::out_of_range
                ("Stack<std::string>::pop(): empty stack");
    }
    cout<<"string special push pop..."<<endl;
    string temp = elems.back();
    elems.pop_back();         // remove last element
    return temp;
}

std::string MyStack<std::string>::top () const
{
    if (elems.empty()) {
        throw std::out_of_range
                ("Stack<std::string>::top(): empty stack");
    }
    cout<<"string special top method..."<<endl;
    return elems.back();      // return copy of last element
}



#endif // MYSTACK2_H_INCLUDED


//#include<iostream>
//#include "myStack2.h"
//using namespace std;

//int main()
//{
//    try
//    {
//        MyStack<int> ms;
//        ms.push(1);
//        ms.push(2);
//        ms.push(3);
//        cout<<"top of ms is "<<ms.top()<<endl;
//        ms.pop();
//        cout<<"top of ms is "<<ms.top()<<endl;
//        ms.pop();
//
//        MyStack<string> mstring;
//        mstring.push("hello");
//        mstring.push("how");
//        mstring.push("are");
//        mstring.push("you");
//
//    }
//    catch(exception const& ex)
//    {
//        cerr<<"Exception has happened: "<<ex.what()<<endl;
//    }
//    return 0;
//}

//output
//top of ms is 3
//top of ms is 2
//string special push method...
//string special push method...
//string special push method...
//string special push method...
